var mongoose = require("mongoose"),
	db = mongoose.connection;
var userSchema = new mongoose.Schema({
	name : {type : String , required : true},
	email : {type : String , required: true},
	password : {type : String , required: true},
	created : { type : Date , default : new Date , required : true , }
});
var userS = db.model("user",userSchema);
//方法
function User(user){
	this.name = user && user.name;
	this.email = user && user.email;
	this.password = user && user.password;
};
//查看是否重复
User.prototype.getUser = function(userName , callback){
	userS.find({name :userName} , function(err , data){
		if(err){
			console.log("错误");
			return callback(err);
		};
		callback(null , data[0]);
	});
};
//保存数据
User.prototype.create = function(callback){
	//组织数据
	var data = {
		name : this.name,
		email : this.email,
		password : this.password
	};
	//执行数据库添加
	userS.create(data , function(error , data){
		if(error){
			console.log(error);
			console.log("失败!");
		}else{
			console.log("保存成功!");
			callback && callback(data);
		};
	});
}
//接口
module.exports = User;
